package com.my.mymafengwo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.model.LatLng;
import com.example.smallstrong.mafengwopro.R;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.my.mymafengwo.bean.Local;
import com.my.mymafengwo.utils.ConstantUtils;
import com.my.mymafengwo.utils.MathUtils;

import java.util.List;

/**
 * ***********************************************
 * author:wgh
 * time:2015/12/5 14:19
 * copyright:
 * name:
 * step:
 * ***********************************************
 */
public class LocalWengsAdapter extends BaseAdapter {

    private Context context;
    private List<Local.DataEntity.WengsEntity.ListEntity> data;
    private double mLat, mLng;
    private BitmapUtils bit;

    public LocalWengsAdapter(Context context, List<Local.DataEntity.WengsEntity.ListEntity> data, double mLat, double mLng) {
        this.context = context;
        this.data = data;
        this.mLat = mLat;
        this.mLng = mLng;
        bit = new BitmapUtils(context);
        bit.configDiskCacheEnabled(true);
        bit.configMemoryCacheEnabled(false);
        bit.configDefaultCacheExpiry(ConstantUtils.CACHEEXPIRY);
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_grid_local_wengs, null);
            holder = new ViewHolder();
            holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.ivLogo = (ImageView) convertView.findViewById(R.id.iv_logo);
            holder.tv = (TextView) convertView.findViewById(R.id.tv_distance);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        bit.display(holder.ivIcon, data.get(position).getImg().getSimg());
        bit.display(holder.ivLogo, data.get(position).getOwner().getLogo(), new BitmapLoadCallBack<ImageView>() {
            @Override
            public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                imageView.setImageBitmap(com.my.mymafengwo.utils.BitmapUtils.getRoundBitmap(bitmap));
            }

            @Override
            public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

            }
        });
        holder.tv.setText(MathUtils.getAMapDistance(new LatLng(mLat, mLng), new LatLng(data.get(position).getLat(), data.get(position).getLng())) / 100 / 10.0 + "km");
//        holder.tv.setText(MathUtils.getOneDouble(MathUtils.getDistance(mLat, mLng, data.get(position).getLat(), data.get(position).getLng())) + "km");

        return convertView;
    }

    public void setData(List<Local.DataEntity.WengsEntity.ListEntity> data) {
        this.data = data;
    }


    class ViewHolder {
        private ImageView ivIcon, ivLogo;
        private TextView tv;
    }
}
